package com.hmall.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.feign.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.RequestCreatePara;
import com.hmall.item.pojo.ResultById;
import com.hmall.item.pojo.RequestParams;
import com.hmall.item.service.IItemService;
import com.hmall.item.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    IdWorker idWorker;

    @Override
    public PageDTO<Item> search(RequestParams params) {
        int page = params.getPage();
        int size = params.getSize();
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
//        Date before = null;
//        Date after = null;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//
//        if (params.getName() != null && !params.getName().equals("")) {
//            wrapper.eq(Item::getName, params.getName());
//        }
//        if (params.getBrand() != null && !params.getBrand().equals("")) {
//            wrapper.eq(Item::getName, params.getBrand());
//        }
//        if (params.getCategory() != null && !params.getCategory().equals("")) {
//            wrapper.eq(Item::getName, params.getCategory());
//        }
//        if (params.getDateRange().length != 0) {
//            try {
//                before = dateFormat.parse(params.getDateRange()[0]);
//                after = dateFormat.parse(params.getDateRange()[1]);
//                wrapper.between(Item::getCreateTime, before, after);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//        }
        Page<Item> page1 = new Page<>(page, size);
        Page<Item> page2 = page(page1, wrapper);
        List<Item> list = page2.getRecords();
        long total = page2.getTotal();
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setList(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    //根据id查询商品
    @Override
    public ResultById searchItemById(Long id) {
        Item item = itemMapper.selectById(id);
        ResultById resultById = new ResultById();
        resultById.setId(item.getId());
        resultById.setBrand(item.getBrand());
        resultById.setCategory(item.getCategory());
        resultById.setImage(item.getImage());
        resultById.setCommentCount(item.getCommentCount());
        resultById.setName(item.getName());
        resultById.setIsAD(item.getIsAD());
        resultById.setPrice(item.getPrice());
        resultById.setSold(item.getSold());
        resultById.setSpec(item.getSpec());
        resultById.setStatus(item.getStatus());
        resultById.setStock(item.getStock());
        return resultById;
    }

    //上架、下架
    @Override
    public void upStatusById(String id, Integer status) {
        itemMapper.upStatusById(id,status);
    }

    //添加商品信息
    @Override
    public void createItem(RequestCreatePara params) {
        Item item = new Item();
        Date date = new Date();
        BeanUtils.copyProperties(params,item);
        long id = idWorker.nextId();
        item.setId(id);
        item.setCreateTime(date);
        item.setUpdateTime(date);
        itemMapper.insert(item);
    }

    //修改商品信息
    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    public void deleteItemById(Long id) {
        itemMapper.deleteById(id);
    }

    @Override
    public void updateStock(Long itemId, Integer num) {
        try {
            update().setSql("stock = stock - " + num).eq("id", itemId).update();
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }
    }
}
